package de.yuv.gsak2iphone.components
{
	import flash.filesystem.*;
	
	public class XmlPreferences
	{
		
		public var preferences:XML;
		private var _prefsFile:File;
		private var _stream:FileStream;
		
		
		public function XmlPreferences(__prefsFilename:String)
		{
			_prefsFile = File.applicationStorageDirectory;
			_prefsFile = _prefsFile.resolvePath(__prefsFilename);
		}
		
		public function save():void{
			var __outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
			__outputString += preferences.toXMLString();
			__outputString = __outputString.replace(/\n/g, File.lineEnding);
			_stream = new FileStream();
			_stream.open(_prefsFile, FileMode.WRITE);
			_stream.writeUTFBytes(__outputString);
			_stream.close();
		}
		
		public function load():void{
			_stream = new FileStream();
			if(_prefsFile.exists)
			{
				_stream.open(_prefsFile, FileMode.READ);
				preferences = XML(_stream.readUTFBytes(_stream.bytesAvailable));
				_stream.close();
			}
			else
			{
				createNewFile();
			}
		}
		
		
		private function createNewFile():void{
			preferences = <preferences/>;
			save();
		}
		
		
	}
}